package com.ec.system.modules.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ec.common.constant.CommonConstant;
import com.ec.core.common.utils.BeanUtilEx;
import com.ec.core.secure.utils.SecureUtil;
import com.ec.system.modules.sys.dto.SysTopMenuDTO;
import com.ec.system.modules.sys.entity.SysTopMenu;
import com.ec.system.modules.sys.mapper.SysTopMenuMapper;
import com.ec.system.modules.sys.service.ISysTopMenuService;
import com.ec.system.modules.sys.vo.SysTopMenuVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: Eric Lee
 * @time: 2021/8/30 14:07
 */
@Service
public class SysTopMenuServiceImpl extends ServiceImpl<SysTopMenuMapper, SysTopMenu> implements ISysTopMenuService {

    @Override
    public List<SysTopMenuVO> getTopMenu() {
        return listTopMenu();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteTopMenu(SysTopMenuDTO sysTopMenuDTO) {
        SysTopMenu sysTopMenu = new SysTopMenu();
        BeanUtil.copyProperties(sysTopMenuDTO,sysTopMenu);
        sysTopMenu.setUserId(SecureUtil.getUser().getUserId());
        return removeById(sysTopMenu);
//        LambdaUpdateWrapper<SysTopMenu> updateWrapper = Wrappers.<SysTopMenu>lambdaUpdate()
//                .set(SysTopMenu::getIsDelete, CommonConstant.ALREADY_DELETED)
//                .eq(SysTopMenu::getUserId, SecureUtil.getUser().getUserId())
//                .eq(SysTopMenu::getTopMenuId, sysTopMenuDTO.getTopMenuId());
//        return this.update(updateWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addTopMenu(SysTopMenuDTO sysTopMenuDTO) {
        SysTopMenu SysTopMenu = new SysTopMenu();
        BeanUtilEx.copyProperties(sysTopMenuDTO, SysTopMenu);
        SysTopMenu.setUserId(SecureUtil.getUser().getUserId());
        return this.save(SysTopMenu);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateTopMenu(SysTopMenuDTO sysTopMenuDTO) {
        LambdaUpdateWrapper<SysTopMenu> updateWrapper = Wrappers.<SysTopMenu>lambdaUpdate()
                .set(SysTopMenu::getTopMenuName, sysTopMenuDTO.getTopMenuName())
                .set(SysTopMenu::getPath, sysTopMenuDTO.getPath())
                .set(SysTopMenu::getSort, sysTopMenuDTO.getSort())
                .set(SysTopMenu::getType, sysTopMenuDTO.getType())
                .set(SysTopMenu::getIcon, sysTopMenuDTO.getIcon())
                .eq(SysTopMenu::getUserId, SecureUtil.getUser().getUserId())
                .eq(SysTopMenu::getTopMenuId, sysTopMenuDTO.getTopMenuId());
        return this.update(new SysTopMenu(), updateWrapper);
    }

    @Override
    public List<SysTopMenuVO> listTopMenu() {
        QueryWrapper<SysTopMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete", CommonConstant.NOT_DELETED);
        wrapper.eq("user_id", SecureUtil.getUser().getUserId());
        List<SysTopMenu> sysTopMenuEntities = baseMapper.selectList(wrapper);
        return convertTopMenuToVo(sysTopMenuEntities);
    }

    @Override
    public boolean configTopMenu(SysTopMenuDTO sysTopMenuDTO) {
        LambdaUpdateWrapper<SysTopMenu> updateWrapper = Wrappers.<SysTopMenu>lambdaUpdate()
                .set(SysTopMenu::getRelationMenuIds, sysTopMenuDTO.getRelationMenuIds())
                .eq(SysTopMenu::getUserId, SecureUtil.getUser().getUserId())
                .eq(SysTopMenu::getTopMenuId, sysTopMenuDTO.getTopMenuId());
        return this.update(updateWrapper);
    }

    private List<SysTopMenuVO> convertTopMenuToVo(List<SysTopMenu> sysTopMenuEntities) {
        List<SysTopMenuVO> topMenuList;
        topMenuList = sysTopMenuEntities.stream().map(e -> {
            SysTopMenuVO sysTopMenuVO = new SysTopMenuVO();
            sysTopMenuVO.setSort(e.getSort());
            sysTopMenuVO.setTopMenuId(e.getTopMenuId());
            sysTopMenuVO.setTopMenuName(e.getTopMenuName());
            sysTopMenuVO.setPath(e.getPath());
            sysTopMenuVO.setCreateTime(e.getCreateTime());
            sysTopMenuVO.setUpdateTime(e.getUpdateTime());
            sysTopMenuVO.setCreateBy(e.getCreateBy());
            sysTopMenuVO.setUpdateBy(e.getUpdateBy());
            sysTopMenuVO.setRelationMenuIds(e.getRelationMenuIds());
            sysTopMenuVO.setType(e.getType());
            sysTopMenuVO.setIcon(e.getIcon());
            return sysTopMenuVO;
        }).sorted(Comparator.comparing(SysTopMenuVO::getSort).reversed()).collect(Collectors.toList());
        return topMenuList;
    }
}
